package com.liuxu.topnews.utils;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.liuxu.topnews.MyApplication;
import com.liuxu.topnews.bean.App;
import com.liuxu.topnews.bean.MultipleItem;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.liuxu.topnews.MyApplication.globalapps;

public class MultiUtils {
    private static MultiUtils Instance;

    public static MultiUtils getInstance() {
        if (Instance == null) {
            Instance = new MultiUtils();
        }
        return Instance;
    }

    public List<App> initAppData(boolean defaultSetting) {
        ArrayList Tempapps = new ArrayList();
        ArrayList Multiapps = new ArrayList();
        AppUtils appUtils=new AppUtils();
        SettingUtils settingUtils=new  SettingUtils();
        List<App> appsets=settingUtils.GetAppSetXmlByboolean(defaultSetting);
        List<App> appinfos=settingUtils.GetAppInfoXml();
        LogUtil.getInstance().show("获取app配置",String.valueOf(appinfos.size()));
        List<PackageInfo> PackageInfos=appUtils.getInstallAppList();
        LogUtil.getInstance().show("已安装app",String.valueOf(PackageInfos.size()));
        Iterator appinfo = appinfos.iterator();
        while(appinfo.hasNext()) {
            Tempapps.add(getAppInfo(PackageInfos, (App)appinfo.next()));
        }
        Iterator tempapp = Tempapps.iterator();
        while(tempapp.hasNext()) {
            Multiapps.add(getAppSet(appsets, (App)tempapp.next()));
        }
        globalapps=Multiapps;
        return Multiapps;
    }
    public List<App> initAppData(List<App> appsets) {
        ArrayList Tempapps = new ArrayList();
        ArrayList Multiapps = new ArrayList();
        AppUtils appUtils=new AppUtils();
        SettingUtils settingUtils=new  SettingUtils();
        List<App> appinfos=settingUtils.GetAppInfoXml();
        LogUtil.getInstance().show("获取app配置",String.valueOf(appinfos.size()));
        List<PackageInfo> PackageInfos=appUtils.getInstallAppList();
        LogUtil.getInstance().show("已安装app",String.valueOf(PackageInfos.size()));
        Iterator appinfo = appinfos.iterator();
        while(appinfo.hasNext()) {
            Tempapps.add(getAppInfo(PackageInfos, (App)appinfo.next()));
        }
        Iterator tempapp = Tempapps.iterator();
        while(tempapp.hasNext()) {
            Multiapps.add(getAppSet(appsets, (App)tempapp.next()));
        }
        globalapps=Multiapps;
        return Multiapps;
    }
    public App getAppInfo(List<PackageInfo> PackageInfos, App app) {
        if (PackageInfos != null && app != null) {
//            LogUtil.getInstance().show("获取app配置信息",String.valueOf(app.zname));
            PackageManager var4 = MyApplication.getContext().getPackageManager();
            Iterator installapps = PackageInfos.iterator();
            while(installapps.hasNext()) {
                PackageInfo installapp = (PackageInfo)installapps.next();
                if (installapp.packageName.equals(app.packname)) {
                    app.icon = installapp.applicationInfo.loadIcon(var4);
                    app.VersionName = installapp.versionName;
                    app.VersionCode = installapp.versionCode;
                    app.IsInstall = true;
                    return app;
                }
            }
        }
        return app;
    }
    public App getAppSet(List<App> appsets, App app) {
        if (appsets != null && app != null) {
            Iterator sets = appsets.iterator();
            while(sets.hasNext()) {
                App set = (App)sets.next();
                if (set.ename.equals(app.ename)) {
                    app.movetype = set.movetype;
                    app.readtype = set.readtype;
                    app.uptime = set.uptime;
                    app.downtime = set.downtime;
                    app.pagenumber = set.pagenumber;
                    app.max_read = set.max_read;
                    app.setCheck(ShareUtil.getInstance().GetCheckBox(app.getZname()));
//                    Log.v(app.zname,String.valueOf(app.IsCheck));
                    return app;
                }
            }
        }
        return app;
    }
    public String getappcount(){
        int total=globalapps.size();
        int installed=0;
        for (App app:globalapps){
            if (app.IsInstall){
                installed+=1;
            }
        }
        return String.valueOf(installed)+"/"+String.valueOf(total);
    }
}
